package com.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.bean.User;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport
{
    public HttpServletResponse getResponse()
    {
        return ServletActionContext.getResponse();
    }

    public HttpServletRequest getRequest()
    {
        return ServletActionContext.getRequest();
    }

    public HttpSession getSession()
    {
        return getRequest().getSession();
    }

    public User getUser()
    {
        return (User) getSession().getAttribute("user");
    }
    
    public String getValue(String name)
    {
        return getRequest().getParameter(name);
    }
    
    /**
     * description
     * @param str tag
     */
    public void outJson(HttpServletResponse resp, String str)
    {
        HttpServletResponse response = resp;
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        // response.setHeader("Cache-Control", "no-cache");
        PrintWriter pw = null;
        try
        {
            pw = response.getWriter();
            pw.println(str);
            pw.flush();
        }
        catch (IOException e)
        {
        }
        finally
        {
            if (null != pw)
            {
                pw.close();
            }
        }
    }
}
